package br.ufrj.dcc.dm.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import br.ufrj.dcc.dm.model.AttributeMetadata;
import br.ufrj.dcc.dm.model.Entry;
import br.ufrj.dcc.dm.util.preprocessors.ArffPreProcessor;

public class ArffDatasetReader extends DatasetReader
{

	private static final String SEPARATOR = ",";
	
	private String datasetPath = "";
	
	private ArffPreProcessor preProcessor;
	
	public ArffDatasetReader(String datasetPath)
	{
		this.datasetPath = datasetPath;
	}
	
	@Override
	public ArrayList<Entry> assembleEntries()
	{
		preProcessor = new ArffPreProcessor(datasetPath);
		
		String outputFilePath = preProcessor.preProcess();
		ArrayList<Entry> entries = new ArrayList<Entry>();
		
		try 
		{
			FileReader fr = new FileReader(outputFilePath);
			BufferedReader inputReader = new BufferedReader( fr );
			String line;
			
			try
			{
				while( (line = inputReader.readLine())!=null )
				{
					entries.add(createEntry(line));
				}
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
			finally 
			{
				inputReader.close();
			}
			
		}
		catch (FileNotFoundException e) 
		{
			System.out.println("Couldn't open file "+ datasetPath);
			e.printStackTrace();
		}
		catch (IOException e)
		{
			System.out.println("Couldn't open file "+ datasetPath);
			e.printStackTrace();
		}
	
		return entries;
	}

	@Override
	protected String getSeparator()
	{
		return SEPARATOR;
	}

	public Map<Integer, AttributeMetadata> retrieveAttributeMetadataMap()
	{
		return preProcessor.getAttributeMetadataMap();
	}
}
